package com.lst.projectlib.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;

/**
 * 自定义的 异常处理类 , 实现了 UncaughtExceptionHandler接口 
 * @author Administrator
 *
 */
public class MyCrashHandler implements UncaughtExceptionHandler {
	// 需求是 整个应用程序 只有一个 MyCrash-Handler 
	private static MyCrashHandler myCrashHandler ;
	private Context context;
	private String logPath;
	
	//1.私有化构造方法
	private MyCrashHandler(){
		
	}
	
	public static synchronized MyCrashHandler getInstance(){
		if(myCrashHandler!=null){
			return myCrashHandler;
		}else {
			myCrashHandler  = new MyCrashHandler();
			return myCrashHandler;
		}
	}
	public void init(Context context,String logPath){
		this.context = context;
		this.logPath = logPath;
	}
	

	public void uncaughtException(Thread arg0, Throwable arg1) {
		// 1.获取当前程序的版本号. 版本的id
//		String versioninfo = getVersionInfo();
//		
//		// 2.获取手机的硬件信息.
//		String mobileInfo  = getMobileInfo();
		
		// 3.把错误的堆栈信息 获取出来 
		final String errorinfo = getErrorInfo(arg1);
		
		try {
			printLog(errorinfo);
		} catch (Exception e) {
			printLog(e.toString() + "  " + errorinfo);
		}
	}
	
	private void printLog(String errorinfo) {
		try {
			// 输出错误日志的路径
			String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + logPath;
			File filePath = new File(dirPath);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}
			LogWriter writer = LogWriter.open(dirPath + "/error_log_" + System.currentTimeMillis() + ".txt");
			writer.print(errorinfo);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//干掉当前的程序 
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	/**
	 * 获取错误的信息 
	 * @param arg1
	 * @return
	 */
	private String getErrorInfo(Throwable arg1) {
		Writer writer = new StringWriter();
		PrintWriter pw = new PrintWriter(writer);
		arg1.printStackTrace(pw);
		pw.close();
		String error= writer.toString();
		return error;
	}

	/**
	 * 获取手机的硬件信息 
	 * @return
	 */
	private String getMobileInfo() {
		StringBuffer sb = new StringBuffer();
		//通过反射获取系统的硬件信息 
		try {

			Field[] fields = Build.class.getDeclaredFields();
			for(Field field: fields){
				//暴力反射 ,获取私有的信息 
				field.setAccessible(true);
				String name = field.getName();
				String value = field.get(null).toString();
				sb.append(name+"="+value);
				sb.append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * 获取手机的版本信息
	 * @return
	 */
	private String getVersionInfo(){
		try {
			PackageManager pm = context.getPackageManager();
			 PackageInfo info =pm.getPackageInfo(context.getPackageName(), 0);
			 return  info.versionName;
		} catch (Exception e) {
			e.printStackTrace();
			return "版本号未知";
		}
	}
}

